package game;

import static utils.StrategyGameUtils.getResourceOutputTypeForFactory;

public class Factory extends Building implements Runnable {

	private int producedQuantityPerSecond;
	private Types outputType;
	private Warehouse inputBuffer;
	private Warehouse outputBuffer;
	private Thread currentThread;

	public Factory(Types type, int capacity, int producedQuantityPerSecond,
			Warehouse inputBuffer, Warehouse outputBuffer) {
		super(type);
		this.producedQuantityPerSecond = producedQuantityPerSecond;
		this.inputBuffer = inputBuffer;
		this.outputBuffer = outputBuffer;
		outputType = getResourceOutputTypeForFactory(type);
	}

	public void run() {
		currentThread = Thread.currentThread();
		while (!currentThread.isInterrupted()) {
			try {
				Resource inputResource = inputBuffer
						.takeOutput(producedQuantityPerSecond);
				Thread.sleep(100);
				System.out.println(inputResource.getType()
						+ " is transformed in " + outputType);
				outputBuffer.addInput(producedQuantityPerSecond);
			} catch (InterruptedException e) {
				currentThread.interrupt();
			}
		}
	}

	public void cancel() {
		currentThread.interrupt();
	}
	
}
